<?php
/**
 * Created by PhpStorm.
 * User: chenzhenlin
 * Date: 2019/3/12
 * Time: 9:06 PM
 */

namespace app\common\exception;

use Exception;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;

class Api extends Handle
{
    public function render(Exception $e)
    {
        $obj = [
            "data" => null,
            "code" => 4001,
            'msg' => $e->getMessage(),
        ];
        // 参数验证错误
        if ($e instanceof ValidateException) {
            $obj['msg'] = $e->getError();
            return json($obj, 200);
        }

        // 请求异常
        if ($e instanceof HttpException && request()->isAjax()) {
            $obj['msg'] = $e->getMessage();
            return response($obj, $e->getStatusCode());
        }

        // 其他错误交给系统处理
//        return parent::render($e);
        return json($obj, 200);

    }

}